import logging
from common.handle_conf import conf
import os
from common.handle_path import LOG_DIR


def create_log(name='my_logger', level='DEBUG', filename='log.log', fh_level='DEBUG', sh_level='DEBUG'):
    # 创建一个日志器，设置接收的等级为debug
    logger = logging.getLogger(name)
    logger.setLevel(level)

    # 设置日志输出的格式
    my_format = logging.Formatter('%(asctime)s - [%(filename)s-->line:%(lineno)d] - %(levelname)s: %(message)s')

    # 设置输出到文件,和输出的等级，格式
    fh = logging.FileHandler(filename,encoding='utf-8')
    fh.setLevel(fh_level)
    fh.setFormatter(my_format)

    # 设置输出到控制台和输出的等级，格式
    sh = logging.StreamHandler()
    sh.setLevel(sh_level)
    sh.setFormatter(my_format)

    # 把设置好fh和sh，添加到日志器中
    logger.addHandler(fh)
    logger.addHandler(sh)

    return logger


my_logger = create_log(
    name=conf.get("logging", 'name'),
    level=conf.get('logging', 'level'),
    filename=os.path.join(LOG_DIR, conf.get("logging", 'filename')),
    sh_level=conf.get('logging', 'sh_level'),
    fh_level=conf.get('logging', 'fh_level'),)